package com.spring.appservice.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.spera.aliyunContent.AliyunContent;
import com.spera.genutil.TokenUtil;
import com.spring.appservice.web.dao.BabyTreeMapper;
import com.spring.appservice.web.service.BabyTreeService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.dao.JedisCache;
import com.spring.tools.MyTools;

@Service
public class BabyTreeServiceImpl implements BabyTreeService{
	
	@Resource
	private MongoTemplate mongoService;
	
	@Resource
	private JedisCache jedisCache;
	
	@Resource
	private BabyTreeMapper babyTreeMapper;
	
	/**
	 *创建成长记录
	 */
	@Override
	public Map<String, Object> createGrowthRecord(String token, String recordTime, 
			String title, String content, String cUserID, MultipartFile file) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}
		String fileID=null;
		
		
		if(file!=null){
		if(!file.isEmpty()){
		HashMap<String, Object> hmx=MyTools.commitFile(file);
		
		if(hmx.get(ApplicationUtils.STATUS).equals("0")){
			return hmx;
		}
		
		
		 fileID=ApplicationUtils.randomUUID();
		
		HashMap hmp=new HashMap();
		hmp.put("fileID", fileID);
		hmp.put("url", hmx.get("filePath"));
		hmp.put("osskey", hmx.get("osskey"));
		hmp.put("bucket", AliyunContent.fileBucket);
		
		
		babyTreeMapper.createFile(hmp);
		}}
		
		String uuid=ApplicationUtils.randomUUID();
		
		HashMap hm=new HashMap();
		hm.put("id", uuid);
		hm.put("recordTime", recordTime);
		hm.put("title", title);
		hm.put("content", content);
		hm.put("cUserID", cUserID);
		hm.put("fileID", fileID);
		
		System.out.println(hm.toString());
		
		int effect = babyTreeMapper.createGrowthRecord(hm);

		if(effect!=1){
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "生成失败");
			return m;
		}
		
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "生成成功");
		
		HashMap hmr=new HashMap();
		hmr.put("id", uuid);
		List list=new ArrayList();
		list.add(hmr);
		
		m.put(ApplicationUtils.DATASET, list);

		return m;
	}
	
	
	/**
	 * 得到记录内容
	 */
	@Override
	public Map<String, Object> getGrowthRecordList(String token,
			String cUserID, String page_size, String page_index, String skip,
			String rows) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		HashMap hmc = MyTools.checkIsNotNull(cUserID);
		if (hmc != null) {
			return hmc;
		}

		HashMap hm = MyTools.pageIndexToSkip(page_size, page_index, skip, rows);
		hm.put("cUserID", cUserID);

		System.out.println(hm.toString());

		List<HashMap> list = babyTreeMapper.getGrowthRecordList(hm);

		m.put(ApplicationUtils.DATASET, list);
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "查询成功");

		return m;
	}
	
	
	
	/**
	 *创建成长数据
	 */
	@Override
	public Map<String, Object> createGrowthDate(String token, String babyBirthday, String recordTime,
			String data, String cUserID) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		HashMap hmc = MyTools.checkIsNotNull(cUserID,babyBirthday,recordTime,data);
		if (hmc != null) {
			return hmc;
		}
		
		
		JSONArray json = JSONArray.fromObject(data);

		// 循环插入数据
		for (int i = 0; i < json.size(); i++) {

			JSONObject jsonObject = json.getJSONObject(i);

			System.out.println(jsonObject.get("math"));
		
			String math=String.valueOf(jsonObject.get("math"));
			String growthDateTypeCode=String.valueOf(jsonObject.get("growthDateTypeCode"));
			
			HashMap hmcx = MyTools.checkIsNotNull(math,growthDateTypeCode);
			if (hmc != null) {
				return hmc;
			}
			
		String uuid=ApplicationUtils.randomUUID();
		
		HashMap hm = new HashMap();
		
		hm.put("id", uuid);
		
		hm.put("cUserID", cUserID);
		hm.put("babyBirthday", babyBirthday);
		hm.put("recordTime", recordTime);
		hm.put("math", math);
		hm.put("growthDateTypeCode", growthDateTypeCode);		
		System.out.println(hm.toString());
		
		int effect = babyTreeMapper.createGrowthDate(hm);

		}
		
		
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "查询成功");
		
		
		
	//	HashMap hmr=new HashMap();
	//	hmr.put("id", uuid);
	//	List list=new ArrayList();
	//	list.add(hmr);
		
	//	m.put(ApplicationUtils.DATASET, list);
		return m;
	}
	
	
	
	@Override
	public Map<String, Object> getGrowthDate(String token, String cUserID, String growthDateTypeCode,String page_size,
			String page_index, String skip, String rows) throws Exception {
		Map<String, Object> m = new HashMap();

		Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
		if (t != null) {
			return t;
		}

		HashMap hmc = MyTools.checkIsNotNull(cUserID);
		if (hmc != null) {
			return hmc;
		}

		HashMap hm = MyTools.pageIndexToSkipSearchAll(page_size, page_index, skip, rows);
		hm.put("cUserID", cUserID);
		hm.put("growthDateTypeCode", growthDateTypeCode);

		System.out.println(hm.toString());

		List<HashMap> list = babyTreeMapper.getGrowthDate(hm);

		m.put(ApplicationUtils.DATASET, list);
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "查询成功");

		return m;
	}
	
	
	
	@Override
	public Map<String, Object> getNormalGrowthDateList(String growthDateTypeCode) throws Exception {
		Map<String, Object> m = new HashMap();

		HashMap hmc = MyTools.checkIsNotNull(growthDateTypeCode);
		if (hmc != null) {
			return hmc;
		}

		HashMap hm = new HashMap();
		hm.put("growthDateTypeCode", growthDateTypeCode);


		System.out.println(hm.toString());

		List<HashMap> list = babyTreeMapper.getNormalGrowthDateList(hm);

		m.put(ApplicationUtils.DATASET, list);
		m.put(ApplicationUtils.STATUS, "1");
		m.put(ApplicationUtils.MSG, "查询成功");

		return m;
	}

}
